﻿Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports ISNet.WebUI.WebGrid
Imports System.Data
Imports log4net

Partial Class F05_Add
    Inherits System.Web.UI.Page

    Public scripttext As String
    Dim lockhash As New Hashtable

    Private Shared logger As ILog = LogManager.GetLogger("F05_Add")

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Session("users") Is Nothing Then
            scripttext = "ClosePop();"
        End If

        Dim prefil2 As String = ""
        prefil2 = Session("prefilter")
        Dim re_adap As New FormLockDSTableAdapters.FundCenterReleasesTableAdapter
        Dim rsql As String = ""
        rsql = " SELECT FundCenterReleases.FundCenter_FundCenterCode FROM FundCenterReleases "
        rsql += " WHERE PlanYear = " & Val(Session("PlanYear")) & " "
        rsql += " AND Version = '" & Session("Versions").ToString & "' "
        rsql += " AND FundCenter_FundCenterCode IN (" & prefil2 & ")" : rsql += " AND ReleaseFlag = 'Y' "
        'Response.Write(rsql)
        'Exit Sub
        Dim re_dt As New FormLockDS.FundCenterReleasesDataTable
        re_adap.GetDataByFilter(re_dt, rsql)
        For Each ritem As FormLockDS.FundCenterReleasesRow In re_dt
            lockhash.Remove(ritem.FundCenter_FundCenterCode) : lockhash.Add(ritem.FundCenter_FundCenterCode, True)
        Next

    End Sub

    Protected Sub FundCenterCombo_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs) Handles FundCenterCombo.InitializeDataSource
        'Dim ds As New DataSet
        'If Session("Versions") > 1 Then
        If IsPostBack = True And Session("Search") = "true" Then
            Dim fctr As String = ""
            fctr = Session("search_fctr")
            Dim adap As New BasicDataTableAdapters.FCTR_Child2TableAdapter
            Dim dt As New BasicData.FCTR_Child2DataTable
            Dim sql = ""
            sql += " WHERE 1 = 1 "
            sql += " and USERNAME = '" & Session("users") & "' AND fctr_view.PlanYear = " & Val(Session("PlanYear")) & " AND FundCenters.PlanYear = " & Val(Session("PlanYear"))
            If fctr <> "" Then
                sql += " AND fctr_view.FundCenterCode IN (" & fctr & ") "
            End If
            sql += " and FundCenterHierarchies.PlanYear = " & Val(Session("PlanYear"))
            sql += "ORDER BY [Level] ASC, FundCenterCode ASC"
            adap.GetDataByFilter(dt, sql)
            e.DataSource = dt
            'Response.Write(sql & "<br />")
            If dt.Count = 1 Then
                FundCenterCombo.Value = dt.Item(0).FundCenterCode
            End If
        Else
            'ds = PublicFunction.GetChild2(Session("users"))
            'ds.Tables(0).DefaultView.Sort = "FundCenterCode ASC"
            'e.DataSource = ds
            Dim adap As New BasicDataTableAdapters.FCTR_Child2TableAdapter
            Dim dt As New BasicData.FCTR_Child2DataTable
            Dim sql = ""
            Dim prefil As String = ""
            prefil = Session("prefilter")
            sql += " WHERE 1 = 1 "
            sql += " and USERNAME = '" & Session("users") & "' AND fctr_view.PlanYear = " & Val(Session("PlanYear")) & " AND FundCenters.PlanYear = " & Val(Session("PlanYear"))
            If prefil <> "" Then
                sql += " AND fctr_view.FundCenterCode IN (" & prefil & ") "
            End If
            sql += " and FundCenterHierarchies.PlanYear = " & Val(Session("PlanYear")) & " "
            sql += "ORDER BY [Level] ASC, FundCenterCode ASC"
            adap.GetDataByFilter(dt, sql)
            e.DataSource = dt
            If dt.Count = 1 Then
                FundCenterCombo.Value = dt.Item(0).FundCenterCode
            End If
        End If

        'Else
        'Dim adap As New BasicDataTableAdapters.FCTR_ChildTableAdapter
        'Dim dt As New BasicData.FCTR_ChildDataTable
        'dt = adap.GetOwnData(Session("FCTR"))
        'e.DataSource = dt
        'End If
    End Sub

    Protected Sub ProductCombo_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs) Handles ProductCombo.InitializeDataSource
        Dim adap As New BasicDataTableAdapters.ProductsTableAdapter
        Dim dt As New BasicData.ProductsDataTable
        dt = adap.GetData(Val(Session("PlanYear")))
        e.DataSource = dt

    End Sub

    Protected Sub SaveDocument_Click(sender As Object, e As EventArgs) Handles SaveDocument.Click
        If FundCenterCombo.Value = "" Then
            scripttext = "alert('กรุณาเลือก Fund Center');"
            Exit Sub
        End If
        If ProductCombo.Value = "" Then
            scripttext = "alert('กรุณาเลือกผลิตภัณฑ์');"
            Exit Sub
        End If
        If lockhash(FundCenterCombo.Value.ToString.Trim) = True Then
            scripttext = "alert('ไม่สามารถบันทึกได้ เนื่องจากมีการส่งแผนไปแล้ว');"
            Exit Sub
        End If
        Try

            Dim f05adap As New F05_DSTableAdapters.BudgetDetailsTableAdapter
            Dim f05dt As New F05_DS.BudgetDetailsDataTable

            Dim adap As New BudgetDetailTableAdapters.BudgetDetailsTableAdapter

            Dim no_list As New Hashtable(14)
            For n As Integer = 1 To 14
                no_list.Add(n, n)
            Next

            f05dt = f05adap.GetData2(FundCenterCombo.Value.ToString, Val(Session("PlanYear")), ProductCombo.Value.ToString, Session("Versions").ToString)
            For Each row As F05_DS.BudgetDetailsRow In f05dt
                Dim ino As Integer = 0
                ino = row.ProductItemNo
                no_list.Remove(ino)
            Next
            Dim plany As Short = 0
            Dim vers As String = ""

            plany = Val(Session("PlanYear"))
            vers = Session("Versions").ToString
            Dim cmmt1 As String = ConfigurationManager.AppSettings("F05_1_CMMT_CODE")
            Dim cmmt2 As String = ConfigurationManager.AppSettings("F05_2_CMMT_CODE")
            If cmmt1 = "" Or cmmt2 = "" Then
                scripttext = "alert('ไม่สามารถระบุ Commitment Item กรุณาติดต่อผู้ดูแลระบบ');ClosePop();"
                Exit Sub
            End If
            'Response.Write(ProductCombo.Value)
            If no_list.Count = 0 Then
                scripttext = "alert('ไม่สามารถเพิ่มข้อมูลซ้ำได้');"
                Exit Sub
            End If
            For Each Itemss As DictionaryEntry In no_list
                Dim prno As Short
                prno = Val(Itemss.Value)
                'Response.Write(Itemss.Value)
                f05adap.InsertF05(plany, plany, vers, "", 0, 0, 0, 0, Nothing, Nothing, Nothing, Nothing, Nothing, 0, "F05", prno, Date.Now, Session("users"), ProductCombo.Value.ToString, plany, FundCenterCombo.Value.ToString, plany, cmmt2, plany)
                'adap.InsertQuery(Session("PlanYear"), Session("PlanYear"), Session("Versions"), "", "", 0, "", 0, 0, 0, 0, Nothing, Nothing, Nothing, Nothing, Nothing, 0, 0, 0, 0, "", "", "F05", "", "", "", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Date.Now(), Nothing, Session("users"), Nothing, Nothing, Nothing, Nothing, Nothing, ProductCombo.Value, 2557, FundCenterCombo.Value, Session("PlanYear"), Nothing, Nothing, "52200240", Session("PlanYear"), Nothing, Nothing, Nothing, 0)
                f05adap.InsertF05(plany, plany, vers, "", Nothing, Nothing, 0, 0, Nothing, Nothing, Nothing, Nothing, Nothing, 0, "F05", prno, Date.Now, Session("users"), ProductCombo.Value.ToString, plany, FundCenterCombo.Value.ToString, plany, cmmt1, plany)
                'adap.InsertQuery(Session("PlanYear"), Session("PlanYear"), Session("Versions"), "", "", 0, "", 0, 0, 0, 0, Nothing, Nothing, Nothing, Nothing, Nothing, 0, 0, 0, 0, "", "", "F05", "", "", "", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Date.Now(), Nothing, Session("users"), Nothing, Nothing, Nothing, Nothing, Nothing, ProductCombo.Value, 2557, FundCenterCombo.Value, Session("PlanYear"), Nothing, Nothing, "52151060", Session("PlanYear"), Nothing, Nothing, Nothing, 0)
            Next

            logger.Info("User " & Session("users") & " insert product " & ProductCombo.Value.ToString & " of fundcenter " & FundCenterCombo.Value.ToString)

            scripttext = "ClosePop();"
        Catch ex As Exception
            logger.Error(ex.Message)
            logger.Error(ex.StackTrace)
        End Try
    End Sub

End Class
